package edu.scau.mis.web.controller.core;
import edu.scau.mis.core.domain.AjaxResult;


import edu.scau.mis.core.oss.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileUploadController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file) {

        // 检查文件是否为图片
        if (!fileService.check(file)) {
            return AjaxResult.error("确认文件格式为图片或文件是否为空,请重新上传");
        }
        // 调用上传方法
        String url = fileService.uploadFile(file);
        return url != null?AjaxResult.success("文件上传成功，文件地址：" + url):AjaxResult.error("文件上传失败");
    }


}
